// let token = localStorage.getItem("token");
let toke = localStorage.getItem("token");
if (toke) {
  let unlogin = document.querySelector("#unlogin");
  unlogin.style.display = "none";
  let logined = document.querySelector("#logined");
  logined.style.display = "block";
}
let back = document.querySelector("#btn_header_logout");
back.onclick = function () {
  localStorage.removeItem("token");
  unlogin.style.display = "block";
  logined.style.display = "none";
};
window.onclick = () => {
  if (!toke) {
    alert("请登录");
    location.href = "./html/login.html";
  }
};
//商品列表
//商品分类id
async function le() {
  let cateid = await myAxios.get(cateAPI);
  console.log(cateid);
}
le();

// let name = "模型手办";
let productArr = [];
//商品列表
async function loadList() {
  // 获取当前选中的分类id
  let page = 1;
  let per = 4;
  let category = 11;
  let res = await myAxios.get(pListAPI, { params: { page, per, category } });
  // .then((r) => {
  //   console.log(r.data);
  //   //   服务返回的数组 添加到product
  //   productArr.push(...r.data.data);
  //   // productArr = r.data.data;
  //   renderList();
  // });
  console.log(res);
  // productArr.push(...r.data.data);
  productArr = res.data.data;
  console.log(productArr);
  renderList();
}
//渲染列表
function renderList() {
  let liArr = productArr.map(
    (v) => `
    <li>
              <a href="./html/detail.html?id=${v.id}"  class="good-link" target="_blank">
                  <img src="http://192.168.18.95:1337${v.coverImage}" width="280" height="268" alt="活动图">
                  <div class="good-info">
                      <p class="good-name">${v.name}</p>
                      <p class="good-pri">￥${v.price}</p>
                  </div>
              </a>
          </li>`
  );
  document.querySelector(".fixlist").innerHTML = liArr.join("");
}
loadList();
async function loadList1() {
  // 获取当前选中的分类id
  let page = 1;
  let per = 4;
  let category = 14;
  let res = await myAxios.get(pListAPI, { params: { page, per, category } });
  // .then((r) => {
  //   console.log(r.data);
  //   //   服务返回的数组 添加到product
  //   productArr.push(...r.data.data);
  //   // productArr = r.data.data;
  //   renderList();
  // });
  console.log(res);
  // productArr.push(...r.data.data);
  productArr = res.data.data;
  console.log(productArr);
  renderList1();
}
//渲染列表
function renderList1() {
  let liArr = productArr.map(
    (v) => `
    <li>
              <a href="./html/detail.html?id=${v.id}"  class="good-link" target="_blank">
                  <img src="http://192.168.18.95:1337${v.coverImage}" width="280" height="268" alt="活动图">
                  <div class="good-info">
                      <p class="good-name">${v.name}</p>
                      <p class="good-pri">￥${v.price}</p>
                  </div>
              </a>
          </li>`
  );
  document.querySelector(".fixlist1").innerHTML = liArr.join("");
}
loadList1();
async function loadList2() {
  // 获取当前选中的分类id
  let page = 1;
  let per = 4;
  let category = 16;
  let res = await myAxios.get(pListAPI, { params: { page, per, category } });
  // .then((r) => {
  //   console.log(r.data);
  //   //   服务返回的数组 添加到product
  //   productArr.push(...r.data.data);
  //   // productArr = r.data.data;
  //   renderList();
  // });
  console.log(res);
  // productArr.push(...r.data.data);
  productArr = res.data.data;
  console.log(productArr);
  renderList2();
}
//渲染列表
function renderList2() {
  let liArr = productArr.map(
    (v) => `
    <li>
              <a href="./html/detail.html?id=${v.id}"  class="good-link" target="_blank">
                  <img src="http://192.168.18.95:1337${v.coverImage}" width="280" height="268" alt="活动图">
                  <div class="good-info">
                      <p class="good-name">${v.name}</p>
                      <p class="good-pri">￥${v.price}</p>
                  </div>
              </a>
          </li>`
  );
  document.querySelector(".fixlist2").innerHTML = liArr.join("");
}
loadList2();
//购物车商品数
let num = document.querySelector(".navico-cartnum");
console.log(num);
let cartArr = [];
async function loadCartList() {
  // 解构 出data属性
  let { data } = await myAxios.get(cartListAPI);
  cartArr = [...data.data];
  console.log(cartArr);
  num.innerHTML = cartArr.length;
}
loadCartList();
